package net.fruit.mq.client.trace.hook;

import org.apache.rocketmq.remoting.protocol.RemotingCommand;

import net.fruit.mq.client.impl.authority.AuthUtil;
import net.fruit.mq.client.impl.authority.SessionCredentials;

public class ClientRPCHook extends AbstractRPCHook {
    private SessionCredentials sessionCredentials;


    public ClientRPCHook(SessionCredentials sessionCredentials) {
        this.sessionCredentials = sessionCredentials;
    }

    @Override
    public void doBeforeRequest(String remoteAddr, RemotingCommand request) {
        byte[] total = AuthUtil.combineRequestContent(request,
            parseRequestContent(request, sessionCredentials.getAccessKey(), sessionCredentials.getMqChannel().name()));
        String signature = AuthUtil.calSignature(total, sessionCredentials.getSecretKey());
        request.addExtField(SessionCredentials.Signature, signature);
        request.addExtField(SessionCredentials.AccessKey, sessionCredentials.getAccessKey());
        request.addExtField(SessionCredentials.MQChannelKey, sessionCredentials.getMqChannel().name());
    }


    @Override
    public void doAfterResponse(String remoteAddr, RemotingCommand request, RemotingCommand response) {

    }
}
